/**
 * 
 */
package cn.com.gxetc.assistant.schedule;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import cn.com.gxetc.assistant.dto.TodoCount;
import cn.com.gxetc.assistant.service.TodoService;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpMessage;

/**
 * @author yfm
 *
 */
@Configurable
@EnableScheduling
@Component
public class ScheduledTasks {

	Logger log = LoggerFactory.getLogger(getClass());

	@Autowired
	TodoService todoService;

	@Autowired
	WxCpService wxCpService;

	// 每1分钟执行一次
	@Scheduled(cron = "0 0 8,15,17 * * ? ")
	public void reportCurrentByCron() {
		log.info("start find user todos ...");

		List<TodoCount> todoCounts = todoService.countByEmployeeIds();
		for (TodoCount todo : todoCounts) {
			WxCpMessage message = WxCpMessage.TEXT().agentId(6).toUser(todo.getUserId())
					.content("您OA目前有(" + todo.getCount() + ")项待办工作").build();
			try {
				wxCpService.messageSend(message);
			} catch (WxErrorException e) {
				log.error("send message fail : {}", e.getMessage());
			}
		}

	}

}
